﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace DnPakExplorer.Controls {
	public partial class TalismanEquipementPanelCtrl : ItemSlotPanelCtrl {

        const string XML_PATH = ".\\Data\\talisman.xml";

		ItemSlotBox mTalisman1;
		ItemSlotBox mTalisman2;
		ItemSlotBox mTalisman3;
		ItemSlotBox mTalisman4;
		ItemSlotBox mTalisman5;
		ItemSlotBox mTalisman6;
		ItemSlotBox mTalisman7;
		ItemSlotBox mTalisman8;
		ItemSlotBox mTalisman9;
		ItemSlotBox mTalisman10;

		ItemSlotBox mCashTalisman1;
		ItemSlotBox mCashTalisman2;
		ItemSlotBox mCashTalisman3;
		ItemSlotBox mCashTalisman4;
		ItemSlotBox mCashTalisman5;

		public TalismanEquipementPanelCtrl() {
			InitializeComponent();

			AddSlot(mTalisman1 = new ItemSlotBox());
			AddSlot(mTalisman2 = new ItemSlotBox());
			AddSlot(mTalisman3 = new ItemSlotBox());
			AddSlot(mTalisman4 = new ItemSlotBox());
			AddSlot(mTalisman5 = new ItemSlotBox());
			AddSlot(mTalisman6 = new ItemSlotBox());
			AddSlot(mTalisman7 = new ItemSlotBox());
			AddSlot(mTalisman8 = new ItemSlotBox());
			AddSlot(mTalisman9 = new ItemSlotBox());
			AddSlot(mTalisman10 = new ItemSlotBox());
			AddSlot(mCashTalisman1 = new ItemSlotBox());
			AddSlot(mCashTalisman2 = new ItemSlotBox());
			AddSlot(mCashTalisman3 = new ItemSlotBox());
			AddSlot(mCashTalisman4 = new ItemSlotBox());
			AddSlot(mCashTalisman5 = new ItemSlotBox());

			mTalisman1.Bounds = new Rectangle(10, 190, 64, 64);
			mTalisman1.TargetSlotId = EquipmentPanel.IS_TALISMAN_2001;
			mTalisman2.Bounds = new Rectangle(70, 190, 64, 64);
			mTalisman2.TargetSlotId = EquipmentPanel.IS_TALISMAN_2002;
			mTalisman3.Bounds = new Rectangle(130, 190, 64, 64);
			mTalisman3.TargetSlotId = EquipmentPanel.IS_TALISMAN_1751;
			mTalisman4.Bounds = new Rectangle(190, 190, 64, 64);
			mTalisman4.TargetSlotId = EquipmentPanel.IS_TALISMAN_1752;
			mTalisman7.Bounds = new Rectangle(70, 250, 64, 64);
			mTalisman7.TargetSlotId = EquipmentPanel.IS_TALISMAN_1251;
			mTalisman8.Bounds = new Rectangle(130, 250, 64, 64);
			mTalisman8.TargetSlotId = EquipmentPanel.IS_TALISMAN_1252;
			mTalisman9.Bounds = new Rectangle(190, 250, 64, 64);
			mTalisman9.TargetSlotId = EquipmentPanel.IS_TALISMAN_1001;
			mTalisman10.Bounds = new Rectangle(250, 250, 64, 64);
			mTalisman10.TargetSlotId = EquipmentPanel.IS_TALISMAN_1002;

			mCashTalisman1.Bounds = new Rectangle(10, 310, 64, 64);
			mCashTalisman1.TargetSlotId = EquipmentPanel.IS_CASH_TALISMAN_200;
			mCashTalisman2.Bounds = new Rectangle(70, 310, 64, 64);
			mCashTalisman2.TargetSlotId = EquipmentPanel.IS_CASH_TALISMAN_175;
			mCashTalisman4.Bounds = new Rectangle(190, 310, 64, 64);
			mCashTalisman4.TargetSlotId = EquipmentPanel.IS_CASH_TALISMAN_125;
			mCashTalisman5.Bounds = new Rectangle(250, 310, 64, 64);
			mCashTalisman5.TargetSlotId = EquipmentPanel.IS_CASH_TALISMAN_100;

			double scale = 0.8f;

			mSlotBackground.ImageSource = ItemSlotPanelCtrl.mSlotBackground.ImageSource;
			mSlotBackground.SourceRect = new Rectangle(0, 0, 69, 69);
			mSlotBackground.DestRect = new Rectangle(0, 0, (int)Math.Round(69*scale), (int)Math.Round(69*scale));

			this.ForAllView<ItemSlotBox>(slot => {
				slot.Background = mSlotBackground;
				slot.SeletedImage = mSelectedImage;
				slot.ItemDestRect = new Rectangle((int)Math.Round(10*scale), (int)Math.Round(10*scale), (int)Math.Round(50*scale), (int)Math.Round(50*scale));
				slot.TypeImage = GetTypeImage(slot.TargetSlotId, new Rectangle((int)Math.Round(10*scale), (int)Math.Round(10*scale), (int)Math.Round(50*scale), (int)Math.Round(50*scale)));
			});

			


			if (File.Exists(XML_PATH)) {
				XmlDocument doc = new XmlDocument();
				doc.Load(XML_PATH);
				XmlNodeList list = doc.SelectNodes("/Root/Slot");
				int i = 0;
				this.ForEachView<ItemSlotBox>(slot => {
					if (i<list.Count) {
						XmlAttributeCollection attrs = list[i++].Attributes;
						slot.Bounds = new Rectangle(
							int.Parse(attrs.GetNamedItem("x").InnerText),
							int.Parse(attrs.GetNamedItem("y").InnerText),
							slot.Bounds.Width,
							slot.Bounds.Height
							);
						return false;
					} else
						return true;
				});
			}

		}


		protected override void SaveLayout() {
			XmlDocument doc = new XmlDocument();
			doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", ""));
			XmlElement rootNode = doc.CreateElement("Root");
			doc.AppendChild(rootNode);
			this.ForAllView<ItemSlotBox>(slot => {
				XmlElement slotNode = doc.CreateElement("Slot");
				slotNode.SetAttribute("x", slot.Bounds.X.ToString());
				slotNode.SetAttribute("y", slot.Bounds.Y.ToString());
				rootNode.AppendChild(slotNode);
			});
			doc.Save(XML_PATH);
		}
	}
}
